<template>
  <fix-area ref="fixArea">
    <div style="width: 100%; display: flex; justify-content: space-between; align-items: flex-start">
      <form-block ref="form" :model="form">
        <el-form-item label="演员" prop="name">
          <h3 style="line-height: 40px;margin: 0;padding: 0">{{ actorDetails.name.trim().length > 0 ? actorDetails.name : '未配置姓名' }}</h3>
        </el-form-item>
      </form-block>
      <el-button type="primary" @click="createVisible = true">添加</el-button>
    </div>
    <div style="flex: 1; min-height: 0; width: 100%">
      <table-block :loading="loading" :data="opusList">
        <table-column label="排序" width="120px" prop="sort" fixed="left" />
        <table-column label="作品名称" prop="name" fixed="left" />
        <table-column label="宣传图" width="120px" prop="cover" :use-slot="true">
          <template #default="{row}">
            <div style="cursor: pointer" @click="$refs.fixArea.previewImg(row.cover)">
              <cell-media :url="row.cover" />
            </div>
          </template>
        </table-column>
        <table-column label="作品状态" width="120px" prop="status" :use-slot="true">
          <template #default="{row}">
            <el-tag :type="row.status === 0 ? 'success' : 'danger'" size="small">{{ row.status === 0 ? '显示' : '隐藏' }}</el-tag>
          </template>
        </table-column>
        <table-column label="操作" width="220px" :use-slot="true" fixed="right">
          <template #default="{row}">
            <el-button size="mini" type="warning" @click="editOpus(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteOpus(row.id)">删除</el-button>
          </template>
        </table-column>
      </table-block>
    </div>
    <pagination :current-page="form.page" :total="total" @current-change="currentChange" />
    <create v-if="createVisible" :id="id" :visible="createVisible" @cancel="createVisible = false" @enter="createEnter" />
    <edit v-if="editVisible" :id="id" :visible="editVisible" :oid="oid" @cancel="editVisible = false" @enter="editEnter" />
  </fix-area>
</template>
<script>
import { actorDetails, opusList, deleteOpus } from '@/api/actor'
import Create from '@/views/actor/opus/component/Create.vue'
import Edit from '@/views/actor/opus/component/Edit.vue'

export default {
  name: 'Opus',
  components: {
    Create, Edit
  },
  props: {
    id: {
      type: String,
      default: '-1'
    }
  },
  data() {
    return {
      actorDetails: {
        name: ''
      },
      createVisible: false,
      opusList: [],
      loading: false,
      form: {
        performer_id: this.id,
        page: 1
      },
      total: 0,
      oid: -1,
      editVisible: false
    }
  },
  async mounted() {
    try {
      this.loading = true
      const actorDetailsRes = await actorDetails({ id: this.id })
      this.actorDetails = actorDetailsRes.data
      try {
        this.loading = true
        const actorOpusListRes = await opusList(this.form)
        this.opusList = actorOpusListRes.data.data
        this.total = actorOpusListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    } catch (error) {
      this.$errorMessage()
      this.loading = false
    }
  },
  methods: {
    async deleteOpus(oid) {
      try {
        await this.$warningConfirm('确认删除?')
        try {
          this.loading = true
          await deleteOpus({ oid })
          try {
            this.loading = true
            const actorOpusListRes = await opusList(this.form)
            this.opusList = actorOpusListRes.data.data
            this.total = actorOpusListRes.data.total
            this.loading = false
          } catch (error) {
            this.$errorMessage()
            this.loading = false
          }
        } catch (error) {
          this.$errorMessage()
        }
      } catch (error) {
        console.log('取消确认删除', error)
      }
    },
    editOpus(oid) {
      this.oid = oid
      this.editVisible = true
    },
    async editEnter() {
      this.editVisible = false
      try {
        this.loading = true
        const actorOpusListRes = await opusList(this.form)
        this.opusList = actorOpusListRes.data.data
        this.total = actorOpusListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async createEnter() {
      this.createVisible = false
      try {
        this.loading = true
        const actorOpusListRes = await opusList(this.form)
        this.opusList = actorOpusListRes.data.data
        this.total = actorOpusListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async currentChange(event) {
      try {
        this.loading = true
        this.form.page = event
        const actorOpusListRes = await opusList(this.form)
        this.opusList = actorOpusListRes.data.data
        this.total = actorOpusListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    }
  }
}
</script>
